package com.czmh.etc.onlinepubsys.services.wj.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czmh.etc.onlinepubsys.entity.wj.CurrentContext;
import com.czmh.etc.onlinepubsys.maper.wj.WjCurrentContextMapper;
import com.czmh.etc.onlinepubsys.services.wj.WjICurrentContextService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class WjCurrentContextServiceImpl implements WjICurrentContextService {
    @Autowired
    private WjCurrentContextMapper currentContextMapper;

    @Override
    public void update(CurrentContext entity) {
        currentContextMapper.updateById(entity);
    }

    @Override
    public void save(CurrentContext entity) {
        currentContextMapper.insert(entity);
    }

    @Override
    public CurrentContext getByUserId(String userId) {
        QueryWrapper<CurrentContext> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("delete_flag", 0);
        //通過支付模式参数区分开新旧数据
        wrapper.eq("pay_mode", 1);
        return currentContextMapper.selectOne(wrapper);
    }

    @Override
    public CurrentContext getByUserId(String userId, Integer payMode) {
        QueryWrapper<CurrentContext> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.gt("state", 0);
        wrapper.eq("delete_flag", 0);
        wrapper.eq("pay_mode", payMode);
        return currentContextMapper.selectOne(wrapper);
    }

    @Override
    public CurrentContext getByOrderNo(String orderNo) {
        QueryWrapper<CurrentContext> wrapper = new QueryWrapper<>();
        wrapper.eq("orderNo", orderNo);
        wrapper.eq("delete_flag", 0);
        wrapper.eq("pay_mode", 1);
        return currentContextMapper.selectOne(wrapper);
    }

    @Override
    public CurrentContext queryByOrderNo(String orderNo) {
        QueryWrapper<CurrentContext> wrapper = new QueryWrapper<>();
        wrapper.eq("orderNo", orderNo);
        wrapper.eq("pay_mode", 1);
        return currentContextMapper.selectOne(wrapper);
    }

    @Override
    public CurrentContext getByOrderNo(String orderNo, Integer payMode) {
        QueryWrapper<CurrentContext> wrapper = new QueryWrapper<>();
        wrapper.eq("orderNo", orderNo);
        wrapper.eq("delete_flag", 0);
        wrapper.eq("pay_mode", payMode);
        return currentContextMapper.selectOne(wrapper);
    }

    @Override
    public void delete(String id) {
        //以下使用逻辑删除而不采用物理删除
        CurrentContext currentContext = currentContextMapper.selectById(id);
        if(currentContext != null){
            currentContext.setDeleteFlag(1);
            currentContextMapper.updateById(currentContext);
        }
        //currentContextMapper.deleteById(id);
    }

    @Override
    public void deleteInvalidContext() {
        //以下使用逻辑删除而不采用物理删除
        QueryWrapper<CurrentContext> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 0);
        wrapper.eq("pay_mode", 1);
        List<CurrentContext> list = currentContextMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                CurrentContext currentContext = list.get(i);
                currentContext.setDeleteFlag(1);
                currentContextMapper.updateById(currentContext);
            }
        }
        //currentContextMapper.delete(wrapper);
    }
}
